<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="page-title">贡献值</div>
          <div class="placeholder"></div>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="content">
        <!-- 贡献值和可用WP -->
        <div class="balance-section">
          <div class="balance-card">
            <div class="balance-item">
              <div class="balance-label">贡献值</div>
              <div class="balance-value">{{ contributionValue }}</div>
            </div>
            <div class="balance-item">
              <div class="balance-label">可用WP</div>
              <div class="balance-value">{{ availableWP }}</div>
            </div>
          </div>
        </div>

        <!-- 兑换按钮 -->
        <div class="action-section">
          <button class="exchange-btn" @click="showExchangeDialog">兑换贡献值</button>
        </div>

        <!-- 贡献值记录 -->
        <div class="record-section">
          <div class="section-header">
            <h3 class="section-title">贡献值记录</h3>
          </div>
          <div class="record-list">
            <div class="empty-record" v-if="records.length === 0">
              <Icon icon="mdi:file-document-outline" class="empty-icon"/>
              <p class="empty-text">暂无贡献值记录</p>
            </div>
            <div class="record-item" v-for="record in records" :key="record.id">
              <div class="record-info">
                <div class="record-type">{{ record.type }}</div>
                <div class="record-time">{{ record.time }}</div>
              </div>
              <div class="record-amount" :class="{ 'positive': record.amount > 0 }">
                {{ record.amount > 0 ? '+' : '' }}{{ record.amount }}
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 兑换贡献值弹窗 -->
      <div v-if="showExchange" class="custom-dialog-mask" @click="showExchange = false">
        <div class="custom-dialog exchange-dialog" @click.stop>
          <div class="dialog-header">
            <h3>兑换贡献值</h3>
            <span class="close-btn" @click="showExchange = false">×</span>
          </div>
          <div class="dialog-body">
            <!-- 兑换数量输入 -->
            <div class="input-section">
              <div class="input-label">兑换数量</div>
              <div class="input-group">
                <input
                  v-model="exchangeAmount"
                  type="number"
                  class="amount-input"
                  placeholder="请输入兑换数量"
                  @input="calculateActualAmount"
                >
              </div>
              <div class="balance-info">
                可用WP数量{{ availableWP }}个 <span class="all-exchange" @click="exchangeAll">全部兑换</span>
              </div>
            </div>

            <!-- 实际到账数量 -->
            <div class="actual-amount-section">
              <div class="amount-info">
                <span class="amount-label">实际到账数量</span>
                <span class="amount-value">{{ actualAmount }}个</span>
              </div>
            </div>

            <!-- 兑换按钮 -->
            <div class="exchange-action">
              <button
                class="exchange-confirm-btn"
                :class="{ disabled: !canExchange }"
                :disabled="!canExchange"
                @click="handleExchange"
              >
                兑换
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'ContributionPage',
  components: {
    Icon
  },
  data() {
    return {
      contributionValue: 0,
      availableWP: 100,
      showExchange: false,
      exchangeAmount: '',
      actualAmount: 0,
      exchangeRate: 1, // 兑换比例，可配置
      records: [] // 贡献值记录
    }
  },
  computed: {
    canExchange() {
      return this.exchangeAmount > 0 &&
        this.exchangeAmount <= this.availableWP
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    handleBack() {
      this.$router.go(-1)
    },

    loadData() {
      // 模拟加载数据
      setTimeout(() => {
        this.contributionValue = 0
        this.records = []
      }, 500)
    },

    showExchangeDialog() {
      this.showExchange = true
      this.exchangeAmount = ''
      this.actualAmount = 0
    },

    calculateActualAmount() {
      if (!this.exchangeAmount || this.exchangeAmount <= 0) {
        this.actualAmount = 0
        return
      }

      // 计算实际到账数量：兑换数量 × 兑换比例
      const amount = parseFloat(this.exchangeAmount)
      this.actualAmount = Math.round(amount * this.exchangeRate * 100) / 100
    },

    exchangeAll() {
      this.exchangeAmount = this.availableWP
      this.calculateActualAmount()
    },

    handleExchange() {
      if (!this.canExchange) return

      console.log('兑换贡献值:', {
        exchangeAmount: this.exchangeAmount,
        actualAmount: this.actualAmount
      })

      // 模拟兑换请求
      alert('兑换申请已提交')

      // 关闭弹窗
      this.showExchange = false

      // 重置表单
      this.exchangeAmount = ''
      this.actualAmount = 0
    }
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.placeholder {
  width: 36px;
}

.content {
  flex: 1;
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.balance-section {
  margin-bottom: 30px;
}

.balance-card {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 12px;
  padding: 20px;
  color: white;
  display: flex;
  justify-content: space-between;
}

.balance-item {
  text-align: center;
  flex: 1;
}

.balance-label {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 8px;
}

.balance-value {
  font-size: 24px;
  font-weight: 600;
}

.action-section {
  margin-bottom: 30px;
}

.exchange-btn {
  width: 100%;
  height: 50px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.exchange-btn:hover {
  background: #5a6fd8;
}

.record-section {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.section-header {
  margin-bottom: 16px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.record-list {
  flex: 1;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  min-height: 200px;
}

.empty-record {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 14px;
  margin: 0;
}

.record-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #e9ecef;
}

.record-item:last-child {
  border-bottom: none;
}

.record-info {
  flex: 1;
}

.record-type {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.record-time {
  font-size: 12px;
  color: #999;
}

.record-amount {
  font-size: 16px;
  font-weight: 600;
  color: #f56565; /* 红色表示支出 */
}

.record-amount.positive {
  color: #48bb78; /* 绿色表示收入 */
}

/* 兑换弹窗样式 */
.custom-dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  border-radius: 20px;
  animation: fadeIn 0.3s ease;
}

.exchange-dialog {
  width: 320px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  font-size: 20px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
  transition: all 0.3s ease;
}

.close-btn:hover {
  color: #666;
  background: #f5f5f5;
  border-radius: 50%;
}

.dialog-body {
  padding: 20px;
}

.input-section {
  margin-bottom: 20px;
}

.input-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.amount-input {
  flex: 1;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 16px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
}

.amount-input:focus {
  border-color: #667eea;
}

.input-unit {
  position: absolute;
  right: 16px;
  font-size: 14px;
  color: #999;
}

.balance-info {
  font-size: 12px;
  color: #999;
}

.all-exchange {
  color: #667eea;
  margin-left: 8px;
  cursor: pointer;
}

.all-exchange:hover {
  text-decoration: underline;
}

.actual-amount-section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
}

.amount-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.amount-label {
  font-size: 14px;
  color: #666;
}

.amount-value {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.exchange-action {
  margin-top: 10px;
}

.exchange-confirm-btn {
  width: 100%;
  height: 44px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.exchange-confirm-btn:hover:not(.disabled) {
  background: #5a6fd8;
}

.exchange-confirm-btn.disabled {
  background: #ccc;
  cursor: not-allowed;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* 响应式设计 */
@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }

  .exchange-dialog {
    width: 280px;
  }
}
</style>
